package com.leetcode;

/**
 * 14. 最长公共前缀
 * 横向扫描
 */
public class Solution14_2 {

    public String longestCommonPrefix(String[] strs) {
        if (strs ==  null || strs.length == 0) {
            return "";
        }
        String prifix = strs[0];
        int count = strs.length;
        for (int i = 1; i < count; i++) {
            prifix =  longestCommonPrefix(prifix, strs[i]);
            if (prifix.length() == 0) {
                break;
            }
        }
        return prifix;
    }

    private String longestCommonPrefix(String str1, String str2) {
        int minLength = Math.min(str1.length(), str2.length());
        int index = 0;
        while (index < minLength && str1.charAt(index) == str2.charAt(index)) {
            index++;
        }
        return str1.substring(0, index);
    }

}
